<?php

namespace MUD;

class R {
    public $dbo;
    public $client;
    public $map;
    public $items;
    public $strings;
    public $logger;
    private $hasher;
    
    private static $_instance=NULL;
    
    private function __construct(){
        \Sock\SocketServer::cLog("R Loaded");
        $this->strings = new Strings();
        //$this->logger = new \MUD\Logger();
        $this->dbo = NULL;
        $this->client=NULL;
        $this->map = new Map;
        $this->items = NULL;
        $this->hasher = new PasswordHash(8, false);
    }
    
    public static function current(){
        if(!is_object(self::$_instance)){
            self::$_instance = new self;
        }
        return self::$_instance;
    }
    
    public function __clone(){
        trigger_error("Cannot clone instance of singleton pattern.", E_USER_ERROR);
    }
    public function __wakeup(){
        trigger_error("Cannot deserialize instance of singleton pattern.", E_USER_ERROR);
    }
    
    public function setClient(&$client){
        $this->client=$client;
    }
    
    public function setItemsRes(&$items){
        $this->items = $items;
    }
    public function setDBInterface(&$dbo){
        $this->dbo = $dbo;
    }
    
    public function send($msg='', $nl="\n", $pointer=true){
        if($pointer){
            $msg.="\n".PHPREG::$client_cursor;
        }else{
            $msg.=$nl;
        }
        
        $this->client->send($msg);
    }
    
    public function broadcast($msg){
        $this->client->sendBroadcast($msg."\n".PHPREG::$client_cursor);
    }
    
    public function read(){
        return trim($this->client->read());
    }
    
    public function encrypt($string){
       // $result = $this->hasher->HashPassword($string);
        return sha1($string);
        //return strlen($result) >= 20 ? $result : false;
    }
    
    public function checkHash($string, $hash){
        return $this->hasher->CheckPassword($string, $hash);
    }
}